<?php

namespace App\Admin\Actions;

use App\Models\Client;
use App\Models\User;
use Carbon\Carbon;
use Encore\Admin\Actions\BatchAction;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Http\Request;

class Sh extends BatchAction
{
    public $name = '审核';
    public $id = '';

    public function handle(Collection $collection,Request $request)
    {
        $status = $request->get('shenhe_status');
        $note = $request->get('shenhe_note');
        $time = $request->get('shenhe_time');

        $ids = [];
        foreach ($collection as $model){
            if($model['real_finished'] != 3 ){// 状态不是A和B
                return $this->response()->error('审核 只能审核 审核中 的客户')->refresh();
            }
            array_push($ids,$model['id']);
        }

        $data = [
            'real_finished'=>$status,
            'shenhe_note'=>$note,
            'shenhe_time'=>$time,
        ];
        if($status == 1){
            $data['employee_status'] = 8;
        }
        $rs = Client::query()->whereIn('id',$ids)->update($data);

        if($rs > 0){
            return $this->response()->success('审核 '. $rs . ' 个客户')->refresh();
        }else{
            return $this->response()->error('审核 '. $rs . ' 个客户')->refresh();
        }
    }

    public function form()
    {
        $this->select('shenhe_status','审核结果')->options([1=>'通过',2=>'驳回']);
        $this->textarea('shenhe_note','审核意见');
        $this->text('shenhe_time')->value(Carbon::now()->toDateTimeString())->readOnly();
    }

}
